<?php
require_once "scripts/config.php";
require_once "scripts/functions.php";


if($sessLoggedIn){
    if(isset($_POST['skinForm'])){
        if($_FILES['fileUpload']['size'] < 20480){
            $namearray = explode(".", $_FILES['fileUpload']['name']);
            
            if(count($namearray) == 2){
                $fileName = $namearray[0] . "-" . $_SESSION['username'] . "." . $namearray[1];
                
                $targetPath = "game/skins/".$fileName;
                
                if(move_uploaded_file($_FILES['fileUpload']['tmp_name'], $targetPath)){
                    $query='
                        Insert Into `'.$config['mysql']['database'].'`.`Skins`
                        (name, description, link)
                        Values
                        ("'.$_POST['skinName'].'", "'.$_POST['skinDesc'].'", "skins/'.basename($targetPath).'");
                    ';
                    
                    runQuery($query);
                    $skinId = mysql_insert_id($config['mysql']['link']);
                    
                    if($skinId != null){
                        $query='
                            Update  `'.$config['mysql']['database'].'`.`ActiveSkin`
                            Set     userId = "'.$_SESSION['userId'].'",
                                    skinId = "'.$skinId.'";
                        ';
                        runQuery($query);
                        $afected = mysql_affected_rows($config['mysql']['link']);
                        
                        if($affected == 0){
                            $query='
                                Insert Into `'.$config['mysql']['database'].'`.`ActiveSkin`
                                (userId, skinId)
                                Values
                                ("'.$_SESSION['userId'].'", "'.$skinId.'");
                            ';
                            runQuery($query);
                        }
                    }
                }
            }
        }
    }
    ?>
<div id="blockArea" class="clear">
    <div class="block">
        <h2 class="manimg">
            Mineshafter Squared Skins Manager
        </h2>
        <div id="bigBoxWrapper">
            <div id="skinUploadBox">
                <form enctype="multipart/form-data" action="skins.php" method="POST">
                    <input type="hidden" name="skinForm" value="true" />
                    <span class="title" style="width:100px;">
                        Upload Skin:
                    </span>
                    <input name="fileUpload" type="file" style="background-color:#0F131F;" />
                    <br />
                    <br />
                    Skin Name: <input type="text" name="skinName" />
                    &nbsp;
                    Description: <input type="text" name="skinDesc" />
                    <input type="submit" value="Upload" />
                </form>
            </div>
        </div>
    </div>
</div>
<?php
} else {
    header("Location:index.php");
}

?>